﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FixtureManageSystem.Models;
using FixtureManageSystem.ViewModels;


namespace FixtureManageSystem.Views
{
    /// <summary>
    /// Employee.xaml 的交互逻辑
    /// </summary>
    public partial class Employee: Page
    {

        public Employee()
        {
            InitializeComponent();
            EmployeeViewMode employeeViewMode = new EmployeeViewMode();
            employeeViewMode.SetDataGrid(this.dataGrid);
            this.DataContext = employeeViewMode;  
        }

        //private void Navigate(int mode)
        //{
        //    EmployeeService ES = new EmployeeService();
        //    // 查询出来的员工总数， 但好像每次导航都要查询一次，后面考虑增加缓存，减少查询次数
        //    int totalCount = ES.GetEmployeesCount();

        //    // 根据点击的按钮，更新员工列表以及显示的数量
        //    switch (mode)
        //    {
        //        case (int)PagingMode.Next:
        //            btnPrev.IsEnabled = true;
        //            btnFirst.IsEnabled = true;
        //            Console.WriteLine(totalCount);
        //            Console.WriteLine(pageIndex);
        //            Console.WriteLine(numberOfRecPerPage);
        //            // 如果总数比现在所在的数量多些
        //            if (totalCount > (pageIndex * numberOfRecPerPage))
        //            {
        //                pageIndex++;
        //                lblpageInformation.Content = pageIndex * numberOfRecPerPage + " of " + totalCount;
        //                // 查询数据
        //                em.employeeSerivce.InquireEmployees(pageIndex, numberOfRecPerPage);
        //                // 数据绑定
        //                em.BindData();
        //            }
        //            // 如果总数恰好等于现在所在的数量
        //            else if (totalCount == (pageIndex * numberOfRecPerPage))
        //            {
        //                lblpageInformation.Content = pageIndex * numberOfRecPerPage + " of " + totalCount;
        //                // 查询数据
        //                em.employeeSerivce.InquireEmployees(pageIndex, numberOfRecPerPage);
        //                // 数据绑定
        //                em.BindData();
        //            }
        //            else
        //            {
        //                btnNext.IsEnabled = false;
        //                btnLast.IsEnabled = false;
        //            }

        //            break;
        //        case (int)PagingMode.Previous:
        //            btnNext.IsEnabled = true;
        //            btnLast.IsEnabled = true;
        //            if (pageIndex > 1)
        //            {
        //                pageIndex -= 1;
        //                lblpageInformation.Content = pageIndex * numberOfRecPerPage + " of " + totalCount;
        //                // 查询数据
        //                em.employeeSerivce.InquireEmployees(pageIndex, numberOfRecPerPage);
        //                // 数据绑定
        //                em.BindData();
        //            }
        //            else
        //            {
        //                btnPrev.IsEnabled = false;
        //                btnFirst.IsEnabled = false;
        //            }
        //            break;

        //        case (int)PagingMode.First:
        //            pageIndex = 2;
        //            Navigate((int)PagingMode.Previous);
        //            break;
        //        case (int)PagingMode.Last:
        //            pageIndex = (totalCount / numberOfRecPerPage);
        //            Navigate((int)PagingMode.Next);
        //            break;

        //        case (int)PagingMode.PageCountChange:  // 初始化
        //            // 页码归到1
        //            pageIndex = 1;
        //            // 每页最大显示数量
        //            numberOfRecPerPage = Convert.ToInt32(cbNumberOfRecords.SelectedItem);
        //            // count 就表示当前位置
        //            lblpageInformation.Content = pageIndex * numberOfRecPerPage + " of " + totalCount;
        //            // 查询数据
        //            em.employeeSerivce.InquireEmployees(pageIndex, numberOfRecPerPage);
        //            // 数据绑定
        //            em.BindData();
        //            btnNext.IsEnabled = true;
        //            btnLast.IsEnabled = true;
        //            btnPrev.IsEnabled = true;
        //            btnFirst.IsEnabled = true;
        //            break;
        //    }
        //}

        //private void cbNumberOfRecords_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    Navigate((int)PagingMode.PageCountChange);
        //}

        //private void btnFirst_Click(object sender, RoutedEventArgs e)
        //{
        //    Navigate((int)PagingMode.First);
        //}

        //private void btnPrev_Click(object sender, RoutedEventArgs e)
        //{
        //    Navigate((int)PagingMode.Previous);
        //}

        //private void btnNext_Click(object sender, RoutedEventArgs e)
        //{
        //    Navigate((int)PagingMode.Next);
        //}

        //private void btnLast_Click(object sender, RoutedEventArgs e)
        //{
        //    Navigate((int)PagingMode.Last);
        //}


        #region 成员变量
        /// <summary>
        /// 父级TabControl
        /// </summary>
        private TabControl m_Parent;
        /// <summary>
        /// 约定的宽度
        /// </summary>
        private double m_ConventionWidth = 100;
        #endregion

        #region 事件
        #region loaded
        /// <summary>
        /// loaded
        /// </summary>
        private void TabItem_Loaded(object sender, RoutedEventArgs e)
        {
            //找到父级TabControl
            m_Parent = FindParentTabControl(this);
            if (m_Parent != null)
                Load();
        }

        #endregion
        #endregion

        private void btn_Close_Click(object sender, RoutedEventArgs e)
        {
            if (m_Parent == null)
                return;

            //移除自身
            m_Parent.Items.Remove(this);
            //移除事件
            m_Parent.SizeChanged -= m_Parent_SizeChanged;

            //调整剩余项大小
            //保持约定宽度item的临界个数
            int criticalCount = (int)((m_Parent.ActualWidth - 5) / m_ConventionWidth);
            //平均宽度
            double perWidth = (m_Parent.ActualWidth - 5) / m_Parent.Items.Count;
            foreach (TabItem item in m_Parent.Items)
            {
                if (m_Parent.Items.Count <= criticalCount)
                {
                    item.Width = m_ConventionWidth;
                }
                else
                {
                    item.Width = perWidth;
                }
            }
        }

        #region Load
        /// <summary>
        /// Load
        /// </summary>
        private void Load()
        {
            //约定的宽度
            double.TryParse(m_Parent.Tag.ToString(), out m_ConventionWidth);
            //注册父级TabControl尺寸发生变化事件
            m_Parent.SizeChanged += m_Parent_SizeChanged;

            //自适应
            //保持约定宽度item的临界个数
            int criticalCount = (int)((m_Parent.ActualWidth - 5) / m_ConventionWidth);
            if (m_Parent.Items.Count <= criticalCount)
            {
                //小于等于临界个数 等于约定宽度
                this.Width = m_ConventionWidth;
            }
            else
            {
                //大于临界个数 每项都设成平均宽度
                double perWidth = (m_Parent.ActualWidth - 5) / m_Parent.Items.Count;
                foreach (TabItem item in m_Parent.Items)
                {
                    item.Width = perWidth;
                }
                this.Width = perWidth;
            }
        }
        #endregion

        #region 递归找父级TabControl
        /// <summary>
        /// 递归找父级TabControl
        /// </summary>
        /// <param name="reference">依赖对象</param>
        /// <returns>TabControl</returns>
        private TabControl FindParentTabControl(DependencyObject reference)
        {
            DependencyObject dObj = VisualTreeHelper.GetParent(reference);
            if (dObj == null)
            {
                return null;
            }
            if (dObj.GetType() == typeof(TabControl))
            {
                return dObj as TabControl;
            }

            else
            {
                return FindParentTabControl(dObj);
            }
        }
        #endregion

        private void m_Parent_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //调整自身大小
            //保持约定宽度item的临界个数
            int criticalCount = (int)((m_Parent.ActualWidth - 5) / m_ConventionWidth);
            if (m_Parent.Items.Count <= criticalCount)
            {
                //小于等于临界个数 等于约定宽度
                this.Width = m_ConventionWidth;
            }
            else
            {
                //大于临界个数 等于平均宽度
                double perWidth = (m_Parent.ActualWidth - 5) / m_Parent.Items.Count;
                this.Width = perWidth;
            }
        }

        private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            Models.Employee em = e.Row.Item as Models.Employee;
            EmployeeViewMode employeeViewMode = this.DataContext as EmployeeViewMode;
            employeeViewMode.UpdateEmployee(em);
            // 调用父类的方法
        }
    }
}
